package sfmc.biz.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import sfmc.biz.service.SfmcOldFileListService;
import sfmc.biz.vo.Sfmc_OLD_FILE_LIST;
import sfmc.common.util.DateUtil;

@Controller
public class SfmcOldFileListController {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Autowired 
	private SfmcOldFileListService sfmcOldFileListService;
	
	@RequestMapping(value ="/old_file_list.do")
	public ModelAndView list(ModelAndView mv, HttpSession session, HttpServletRequest request, Sfmc_OLD_FILE_LIST sfmc_OLD_FILE_LIST){

		mv.setViewName("/main/main_old_list");
		
		if("".equals(sfmc_OLD_FILE_LIST.getSearchFromDate())){
			sfmc_OLD_FILE_LIST.setSearchFromDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
			sfmc_OLD_FILE_LIST.setSearchToDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
			sfmc_OLD_FILE_LIST.setPageNo(1);
			sfmc_OLD_FILE_LIST.setPageSize(20);
			sfmc_OLD_FILE_LIST.setOffSet(0);
			sfmc_OLD_FILE_LIST.setTotalCnt(0);
		}else{
			sfmc_OLD_FILE_LIST.setOffSet((sfmc_OLD_FILE_LIST.getPageNo()-1)*sfmc_OLD_FILE_LIST.getPageSize());
			sfmc_OLD_FILE_LIST.setTotalCnt(sfmcOldFileListService.getOldRecordCount(sfmc_OLD_FILE_LIST));
		}

		List<Sfmc_OLD_FILE_LIST> oldFileList = sfmcOldFileListService.getOldRecordList(sfmc_OLD_FILE_LIST);
		
		mv.addObject("oldFileList", (new Gson()).toJson(oldFileList));
		mv.addObject("oldFiledata", sfmc_OLD_FILE_LIST);
		
		return mv;
	}
}